﻿using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using XinLife.Core.Extention;
using XinLife.Model;

namespace XinLife.Core.Helper
{
    public class AspNetUser : IUser
    {
        private readonly IHttpContextAccessor _accessor;
        private readonly ILogger<AspNetUser> _logger;

        //声明一个 IServiceCollection 接口类
        public static IServiceCollection? serviceCollection;

        public AspNetUser(IHttpContextAccessor accessor, ILogger<AspNetUser> logger)
        {
            _accessor = accessor;
            _logger = logger;
        }
        /// <summary>
        /// 获取当前用户
        /// </summary>
        public static User User
        {
            get
            {
                return GetUser();
            }
            set
            {
                IHttpContextAccessor accessor = (IHttpContextAccessor)serviceCollection.BuildServiceProvider().GetService(typeof(IHttpContextAccessor));
                accessor.HttpContext.Items["user"] = value;
            }
        }
        private static User GetUser()
        {
            IHttpContextAccessor accessor = (IHttpContextAccessor)serviceCollection.BuildServiceProvider().GetService(typeof(IHttpContextAccessor));
            return accessor.HttpContext.Items["user"] as User;
        }

        public static string UserId => GetUserId();
        /// <summary>
        /// 获取用户id
        /// </summary>
        /// <returns></returns>
        public static string GetUserId()
        {
            IHttpContextAccessor accessor = (IHttpContextAccessor)serviceCollection.BuildServiceProvider().GetService(typeof(IHttpContextAccessor));
            return accessor.HttpContext.User.Claims.First(t => t.Type == ClaimTypes.NameIdentifier).Value;
        }




        public bool IsAuthenticated()
        {
            return _accessor.HttpContext.User.Identity.IsAuthenticated;
        }

        public string GetToken()
        {
            return _accessor.HttpContext?.Request?.Headers["Authorization"].ToString().Replace("Bearer ", "");
        }

        public List<string> GetUserInfoFromToken(string ClaimType)
        {
            var jwtHandler = new JwtSecurityTokenHandler();
            var token = "";

            token = GetToken();
            // token校验
            if (!token.IsEmpty() && jwtHandler.CanReadToken(token))
            {
                JwtSecurityToken jwtToken = jwtHandler.ReadJwtToken(token);

                return (from item in jwtToken.Claims
                        where item.Type == ClaimType
                        select item.Value).ToList();
            }

            return new List<string>() { };
        }

        public IEnumerable<Claim> GetClaimsIdentity()
        {
            return _accessor.HttpContext.User.Claims;
        }
    }
}
